package cn.neusoft.luofa603.yuejian_29.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.neusoft.luofa603.yuejian_29.Global;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.bean.Training;

/**
 * Created by Administrator on 2017/11/6.
 */

public class CainiaoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    private CainiaoAdapter.MyItemClickListener mItemClickListener;


    public CainiaoAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ImageView train_pic;
        TextView train_name,number;
        private CainiaoAdapter.MyItemClickListener mListener;

        public ViewHolder(View itemView,CainiaoAdapter.MyItemClickListener myItemClickListener){

            super(itemView);
            train_pic = (ImageView)itemView.findViewById(R.id.tain_img);
            train_name = (TextView)itemView.findViewById(R.id.tain_name);
            number=(TextView)itemView.findViewById(R.id.number);

            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
        }

        public void onClick(View view) {
            if (mListener != null) {
                mListener.onItemClick(view, getPosition());
            }

        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.item_cainiao,parent,false);
        CainiaoAdapter.ViewHolder holder = new CainiaoAdapter.ViewHolder(v, mItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final Training entity = (Training)mDataList.get(position);
        if (null==entity)
            return;
       CainiaoAdapter.ViewHolder viewHolder = (CainiaoAdapter.ViewHolder) holder;
        viewHolder.train_name.setText(entity.getName());
        viewHolder.number.setText(entity.getHits());

        String photo = entity.getThumb();
        Picasso.with(mContext)
                .load(Global.PIC+photo)
                .into(viewHolder.train_pic);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemClickListener(CainiaoAdapter.MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }

}
